import inputCommon from '../../../../mixin/inputCommon'
import ElUpload from './upload'
import requiredLabel from '../../../../components/requiredLabel'
import _ from 'lodash'
import { addFile } from '../../../../service'

export default {
  name: 'render',
  components: { ElUpload, requiredLabel },
  props: ['config', 'editor', 'scope', 'isMobile'],
  inject: ['rootForm'],
  mixins: [inputCommon],
  data() {
    return {
      imgSrc: undefined,
      fullViewShow: false,
      fullViewImg: '',
      imgs: [],
      fileUrl: window.env.fileUrl
    }
  },
  created() {
    if(this.scope) {
    }else{
      this.rootForm.properties[this.config.propertyName] = []
    }
  },
  methods: {
    /**
     * 上传文件
     */
    uploadFile(param) {
      const fileSize = Math.round((param.file.size * 100) / (1024 * 1024)) / 100
      const types = ['png', 'jpg', 'jpeg', 'PNG', 'JPEG', 'JPG']
      const fileName = param.file.name

      const isType = types.some(type => {
        return param.file.type.indexOf(type) >= 0
      })

      if (fileSize > 1) {
        this.$message({
          type: 'warning',
          message: '文件大小不可超过1MB !'
        })
        if(this.scope){
          this.scope[this.config.propertyName].pop()

        }else{

          this.rootForm.properties[this.config.propertyName].pop()
        }
        return
      }

      if (fileName.length && fileName.length >= 100) {
        this.$message({
          type: 'warning',
          message: '文件名不可超过100个字符 !'
        })
        if(this.scope){
          this.scope[this.config.propertyName].pop()

        }else{

          this.rootForm.properties[this.config.propertyName].pop()
        }
        return
      }

      if (!isType) {
        this.$message({
          type: 'warning',
          message: '不支持此文件格式 !'
        })
        if(this.scope){

          this.scope[this.config.propertyName].pop()
        }else{

          this.rootForm.properties[this.config.propertyName].pop()
        }
        return
      }
      let file
      file = new FormData()
      file.append('file', param.file)
      file.append('fileName', fileName)
      file.append('description', '')
      file.append('tagIds', [])

      const loading = this.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      addFile({
        factoryId: window.localStorage.factoryId,
        formData: file
      })
        .then(res => {
          // this.rootForm.properties[this.config.propertyName].push(res.data.filePath)this.scope[this.config.propertyName]
          if(this.scope){
            this.$set(this.scope[this.config.propertyName][this.scope[this.config.propertyName].length-1], 'url', res.data.filePath)

          }else{

            this.$set(this.rootForm.properties[this.config.propertyName][this.rootForm.properties[this.config.propertyName].length-1], 'url', res.data.filePath)
          }
          loading.close()
        })
        .catch(err => {
          if(this.scope){
            this.scope[this.config.propertyName].pop()

          }else{

            this.rootForm.properties[this.config.propertyName].pop()
          }
          loading.close()
        })
    },
    handleChange(file, fileList) {
      var lastFileName = file.name.split('.')[file.name.split('.').length - 1]
      var imgType = ['jpg', 'jpeg', 'png']
      if (imgType.indexOf(lastFileName.toLowerCase()) < 0) {
        this.$message({
          message: '请选择图片上传',
          type: 'warning'
        })
        _.remove(fileList, one => one.uid === file.uid)
        return
      }
      if(this.scope){
        this.scope[this.config.propertyName].push(file)

      }else{

        this.rootForm.properties[this.config.propertyName].push(file)
      }
      // this.$refs.formItem.$emit('el.form.change', this.rootForm.properties[this.config.propertyName])
    },
    handleRemove(index) {
      if(this.scope){
        this.scope[this.config.propertyName].splice(index, 1)

      }else{

        this.rootForm.properties[this.config.propertyName].splice(index, 1)
      }
      // this.$refs.formItem.$emit('el.form.change', this.rootForm.properties[this.config.propertyName])
    }
  }
}
